package com.chouxiaozi.openfeigndemo.produce;

import com.chouxiaozi.openfeigndemo.vo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用于提供接口请求，便于测试
 * @Author ws
 * @create 2023/4/21 16:47
 */
@RestController
@RequestMapping("/produce")
public class ProduceController {

    @PostMapping("/post1")
    public String post1(@RequestBody User user) throws JsonProcessingException {
        return new JsonMapper().writeValueAsString(user);
    }

    @PostMapping("/post2")
    public String post2(@RequestBody User user, HttpServletRequest request) throws JsonProcessingException {
        String authorization = request.getHeader("authorization");
        return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization;
    }

    @PostMapping("/post3")
    public String post4(@RequestBody User user, HttpServletRequest request,
                        @RequestParam("gg") String gg) throws JsonProcessingException {
        String authorization = request.getHeader("authorization");
        return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization + ", gg="+ gg;
    }

    @PostMapping("/post4/{userId}")
    public String post4(@RequestBody User user, HttpServletRequest request,
                        @PathVariable String userId,
                        @RequestParam("gg") String gg) throws JsonProcessingException {
        String authorization = request.getHeader("authorization");
        return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization + ", userId=" + userId + ", gg="+ gg;
    }

    @PostMapping("/post5")
    public String post5(User user) throws JsonProcessingException {
        return new JsonMapper().writeValueAsString(user);
    }

    @GetMapping("/post6")
    public String post6(User user) throws JsonProcessingException {
        return new JsonMapper().writeValueAsString(user);
    }
}
